﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<DTO.Models.FEEDBACK>>" %>


<div class="dung-right">
    <input id="removelist" type="button" class="dung-button" title="Delete selected item" onclick="removeFeedbacks()" value="Xóa">&nbsp;
</div>
<div class="clearer">&nbsp;</div>

<div>
	<form action="" method="post" id="postlist">
	    <table width="100%" class="dung-table">
		    <tr>
			    <th width="5%"></th>
			    <th width="15%">Người liên hệ</th>
                <th width="15%">Email liên hệ</th>
			    <th width="40%">Nội dung liên hệ</th>
			    <th width="15%">Ngày liên hệ</th>
                <th width="10%">Trạng thái</th>
		    </tr>
		    <tbody>
                <%
                    foreach (var obj in Model)
                    {
                %>
                        <tr>
                            <td>
                                <%= Html.CheckBox("mediaidselect", false, new { value = obj.FeedbackId, @class = "selectDelete" })%>
                            </td>
                            <td><%= Html.Encode(obj.Name) %></td>
                            <td><%= Html.Encode(obj.Email) %></td>
                            <td><%= Html.Encode(obj.FeedbackContent) %></td>
                            <td>
                                <%= Html.Encode(string.Format("{0:dd/MM/yyyy HH:mm:ss tt}", obj.Date)) %>
                            </td>
                            <td>
                                <%  bool isAnswered = obj.IsAnswered ?? false;
                                    if (isAnswered)
                                    {%>
                                    Đã trả lời
                                <%}
                                    else
                                    {%>
                                    <a class="dung-button" title="Answer selected item" onclick="answerFeedback(<%=obj.FeedbackId%>)">Trả lời</a>
                                <%} %>
                            </td>
                        </tr>
                <%         
                    }
                %>
		    </tbody>
	    </table>
    </form>
</div>

<div>
    <div class="dung-right">
    <% 
        int totalpage = Convert.ToInt32(ViewData["TotalPage"]);
        int pageindex = Convert.ToInt32(ViewData["PageIndex"]);
        int pagesize = Convert.ToInt32(ViewData["PageSize"]);
        
            
        int th = pageindex / pagesize;
        int begin = 1;
        int max = totalpage;
        if(th - 1 > 0)
        {
            begin = (th - 1) * pagesize;
        }
        if((th + 1) * pagesize < totalpage)
        {
            max = (th + 1) * pagesize;   
        }
    %>
    <% Response.Write("<input type='button' class='dung-button' onclick='loadmediapage(" + 1 + ")' value='|<' />");%>
    <%    
        for(int index = begin; index <= max; index++)
        {
            if(index != pageindex)
            {
                if((index == begin && index % pagesize == 0) || (index == max && index % pagesize == 0))
                {
                    %>
                    <% Response.Write("<input type='button' class='dung-button' onclick='loadmediapage(" + index.ToString() + ")' value='...' />"); %>
                    <%
                }
                else
                {
                    %>
                    <% Response.Write("<input type='button' class='dung-button' onclick='loadmediapage(" + index.ToString() + ")' value='" + index.ToString() +"' >"); %>
                    <%
                }
            }    
            else
            {
                %>
                <% Response.Write("<input type='button' class='dung-button-other' onclick='loadmediapage(" + index.ToString() + ")' value='" + index.ToString() + "' />"); %>
                <%
            }
        }
    %>
	<% Response.Write("<input type='button' class='dung-button' onclick='loadmediapage(" + (totalpage).ToString() + ")' value='>|' />");%>
    
</div>
<div class="clearer">&nbsp;</div>
</div>

<script type="text/javascript">

    function loadmediapage(pageindex) {
        var status = $("#statuslist").val() + "";
        if (status.indexOf("all") == -1) {
            search(pageindex);
        }
        else {
            viewall(pageindex);
        }
    }

    function removeFeedbacks() {
        var listid = "";
        $("input.selectDelete:checked").each(function () {
            listid += $(this).val() + "-";
        });
        if (listid.length == 0) {
            alert("Vui lòng chọn một item để xóa");
        }
        else {
            if (confirm("Bạn có chắc muốn xóa không?") == true) {
                $.get("/Feedback/Delete/" + listid,
                    function (data) {
                        loadmediapage(<%= ViewData["PageIndex"] %>);
                    });
            }
        }
    }

    function answerFeedback(id) {
        if (id > 0) {
            if (confirm("Bạn đã trả lời thông tin phản hồi này?") == true){
                $.get("/Feedback/Answer/" + id,
                    function (data) {
                        loadmediapage(<%= ViewData["PageIndex"] %>);
                    });
            }
        }
    }
</script>